<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title"
      :width="modalWidth"
      :destroyOnClose="true"
      @cancel="handleClose"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <div
        :style="{
          width: '100%',
          border: '1px solid #e9e9e9',
          padding: '10px 16px',
          background: '#fff',
        }"
      >
        <a-spin :spinning="confirmLoading">
          <a-form-model ref="ruleForm" :model="model" :rules="rules">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="工序序号"
              prop="workStageId"
              hasFeedback
            >
              <a-input
                placeholder="请输入工序序号"
                v-model.trim="model.workStageId"
              />
            </a-form-model-item>

            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="工序名称"
              hasFeedback
            >
              <a-input
                placeholder="请输入工序名称"
                v-model.trim="model.workStageName"
              />
            </a-form-model-item>
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="参考单价"
              hasFeedback
            >
              <a-input
                placeholder="请输入单价"
                v-model.trim="model.referencePrice"
              />
            </a-form-model-item>

            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="部门"
              hasFeedback
            >
              <a-tree-select
                v-model.trim="model.userDepartment"
                style="width: 100%"
                :dropdown-style="{
                  maxHeight: '400px',
                  overflow: 'auto',
                }"
                :tree-data="treeData"
                :replaceFields="replaceFields"
                placeholder="请选择部门"
                tree-default-expand-all
              ></a-tree-select>
            </a-form-model-item>

            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="工序内容"
              hasFeedback
            >
              <a-input
                placeholder="请输入工序内容"
                v-model.trim="model.workStageContent"
              />
            </a-form-model-item>

            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="负责人"
              hasFeedback
            >
              <a-input
                placeholder="请输入负责人"
                v-model.trim="model.responsiblePerson"
              />
            </a-form-model-item>
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="工序时间"
              hasFeedback
            >
              <a-date-picker
                :showToday="false"
                placeholder="请选择工序时间"
                style="width:100%"
                @change="
                  (date, dateString) =>
                    onChange(date, dateString, 'processTime')
                "
                v-model.trim="model.processTime"
              />
            </a-form-model-item>

            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="备注"
              hasFeedback
            >
              <a-input placeholder="请输入工序内容" v-model.trim="model.note" />
            </a-form-model-item>
          </a-form-model>
        </a-spin>
      </div>
    </a-modal>
  </div>
</template>

<script>
import api from "@/api/apiServer";

export default {
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 700, // 抽屉宽度
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      rules: {
        workStageId: [{ required: true, message: "请输入工序序号" }],
      },
      replaceFields: {
        title: "departmentName",
        key: "id",
        value: "id",
        children: "childList",
      },
      treeData: [],
    };
  },

  methods: {
    async DepartmentTree() {
      const res = await api.DepartmentTree();
      this.treeData = res.data;
    },
    // 新增
    modalAdd() {
      this.modalEdit({});
    },
    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      this.visible = true;
      this.DepartmentTree();
      this.model = Object.assign({}, record);
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.$refs.ruleForm.resetFields();
    },

    handleOk() {
      const that = this;
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.handleSubmit();
        } else {
          return false;
        }
      });
    },
    onChange(date, dateString, key) {
      this.$set(this.model, key, dateString);
    },
    async handleSubmit() {
      let res;
      this.confirmLoading = true;
      //根据id判断是修改还是新增
      if (this.model.id) {
        res = await api.workUpdate(this.model); //修改
      } else {
        res = await api.workInsert(this.model); //新增
      }
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        return;
      }
      this.$message.success(res.message);
      this.confirmLoading = false;
      this.$emit("ok");
      this.visible = false;
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 700;
      }
    },
  },
};
</script>

<style></style>
